package cn.edu.scau.cmi.huangxiaodong.client;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import cn.edu.scau.cmi.huangxiaodong.safeComposite.Organization;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputDialog;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class ConsistentCompositeClient extends Application {
	  List<Organization> employees = Arrays.<Organization>asList();
	     TreeItem<String> rootNode = 
	         new TreeItem<>("RootTeam");
	  
	     public static void main(String[] args) {
	         Application.launch(args);
	     }
	  
	     @Override
	     public void start(Stage stage) {
	         rootNode.setExpanded(true);
	  
	            
	     //  System.out.print(rootNode.getValue());
	         stage.setTitle("一致性组合模式");
	         VBox box = new VBox();
	         final Scene scene = new Scene(box, 400, 300);
	         scene.setFill(Color.LIGHTGRAY);
	         TreeView<String> treeView = new TreeView<>(rootNode);
	         treeView.setEditable(true);
	         treeView.setCellFactory((TreeView<String> p) -> 
	             new TextFieldTreeCellImpl());
	          
	         box.getChildren().add(treeView);
	         stage.setScene(scene);
	         stage.show();
	     }
	  
	     private final class TextFieldTreeCellImpl extends TreeCell<String> {
	  
	         private TextField textField;
	         private final ContextMenu addMenu = new ContextMenu();
	         private final ContextMenu deleteMenu = new ContextMenu();
	         
	         public TextFieldTreeCellImpl() {
	             MenuItem addTeamMenuItem = new MenuItem("Add Team");
	             MenuItem addPersonMenuItem = new MenuItem("Add Person");
	             MenuItem deleteMenuItem = new MenuItem("Delete");
	             MenuItem deleteMenuItem0 = new MenuItem("Delete");
	             deleteMenu.getItems().add(deleteMenuItem0 );
	             addMenu.getItems().addAll(addTeamMenuItem ,addPersonMenuItem,deleteMenuItem);
	             addTeamMenuItem.setOnAction((ActionEvent t) -> {
	             
	                     TextInputDialog dialog = new TextInputDialog( "T");
	                     dialog.setTitle("组织添加界面");
	                     dialog.setContentText("请输入团体名:");
	                     Optional<String> result = dialog.showAndWait();
	                     //确认输入了内容，避免NPE
	                     String teamString=null;
	                     if (result.isPresent()) {                  
	                        teamString = result.get();
	                        TreeItem newOrganization = 
	                                new TreeItem<>(teamString);
	                        	newOrganization.setExpanded(true);
	                            getTreeItem().getChildren().add(newOrganization);              
	                     }              
	             });
	             
	             addPersonMenuItem.setOnAction((ActionEvent t) -> {
	              
	                 TextInputDialog dialog = new TextInputDialog("P");
	                 dialog.setTitle("组织添加界面");
	                 dialog.setContentText("请输入个人名:");
	                 Optional<String> result = dialog.showAndWait();
	                 //确认输入了内容，避免NPE
	                 String personString=null;
	                 if (result.isPresent()) {                  
	                	 personString = result.get();
	                    TreeItem newOrganization = 
	                            new TreeItem<>(personString);
	                    	newOrganization.setExpanded(false);
	                        getTreeItem().getChildren().add(newOrganization);              
	                 }                  
	             });   
	             
	             deleteMenuItem.setOnAction((ActionEvent t) -> {       	
	            	 getTreeItem().getParent().getChildren().remove(getTreeItem());
	            	             	 
	              });   
	     deleteMenuItem0.setOnAction((ActionEvent t) -> {
	            	getTreeItem().getParent().getChildren().remove(getTreeItem());
	            	           	 
	              });   
	             
	         }
	        
	         @Override
	         public void startEdit() {
	             super.startEdit();
	  
	             if (textField == null) {
	                 createTextField();
	             }
	             setText(null);
	             setGraphic(textField);
	             textField.selectAll();
	         }
	  
	         @Override
	         public void cancelEdit() {
	             super.cancelEdit();
	  
	             setText((String) getItem());
	             setGraphic(getTreeItem().getGraphic());
	         }
	  
	         @Override
	         public void updateItem(String item, boolean empty) {
	             super.updateItem(item, empty);
	  
	             if (empty) {
	                 setText(null);
	                 setGraphic(null);
	             } else {
	                 if (isEditing()) {
	                     if (textField != null) {
	                         textField.setText(getString());
	                     }
	                     setText(null);
	                     setGraphic(textField);
	                 } else {
	                     setText(getString());
	                     setGraphic(getTreeItem().getGraphic());
	                    // System.out.println(getTreeItem().getValue());
	                     if (
	                     		getTreeItem().isExpanded()
	                     ){
	                         setContextMenu(addMenu);
	                     }else 
	                     if(!getTreeItem().isExpanded()) {
	                    	 setContextMenu(deleteMenu);
	                     }
	                 }
	             }
	         }
	         
	         private void createTextField() {
	             textField = new TextField(getString());
	             textField.setOnKeyReleased((KeyEvent t) -> {
	                 if (t.getCode() == KeyCode.ENTER) {
	                     commitEdit(textField.getText());
	                 } else if (t.getCode() == KeyCode.ESCAPE) {
	                     cancelEdit();
	                 }
	             });  
	             
	         }
	  
	         private String getString() {
	             return getItem() == null ? "" : getItem().toString();
	         }
	     }
	  
	    
}
